2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// Private functions for Mini-XML, a small XML file parsing library.
|
|
|
|
|
//
|
|
|
|
|
// https://www.msweet.org/mxml
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2003-2024 by Michael R Sweet.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
|
|
|
|
// information.
|
|
|
|
|
//
|
2003-09-28 21:09:04 +00:00
|
|
|
|
|
2007-09-21 04:46:02 +00:00
|
|
|
|
#include "mxml-private.h"
|
2003-12-03 03:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// Some crazy people think that unloading a shared object is a good or safe
|
|
|
|
|
// thing to do. Unfortunately, most objects are simply *not* safe to unload
|
|
|
|
|
// and bad things *will* happen.
|
|
|
|
|
//
|
|
|
|
|
// The following mess of conditional code allows us to provide a destructor
|
|
|
|
|
// function in Mini-XML for our thread-global storage so that it can possibly
|
|
|
|
|
// be unloaded safely, although since there is no standard way to do so I
|
|
|
|
|
// can't even provide any guarantees that you can do it safely on all platforms.
|
|
|
|
|
//
|
|
|
|
|
// This code currently supports AIX, HP-UX, Linux, macOS, Solaris, and
|
|
|
|
|
// Windows. It might work on the BSDs and IRIX, but I haven't tested that.
|
|
|
|
|
//
|
2010-09-19 04:56:51 +00:00
|
|
|
|
|
|
|
|
|
#if defined(__sun) || defined(_AIX)
|
|
|
|
|
# pragma fini(_mxml_fini)
|
|
|
|
|
# define _MXML_FINI _mxml_fini
|
|
|
|
|
#elif defined(__hpux)
|
|
|
|
|
# pragma FINI _mxml_fini
|
|
|
|
|
# define _MXML_FINI _mxml_fini
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#elif defined(__GNUC__) // Linux and macOS
|
2010-09-19 04:56:51 +00:00
|
|
|
|
# define _MXML_FINI __attribute((destructor)) _mxml_fini
|
|
|
|
|
#else
|
|
|
|
|
# define _MXML_FINI _fini
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#endif // __sun
|
2010-09-19 04:56:51 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-07 19:06:50 +00:00
|
|
|
|
//
|
|
|
|
|
// 'mxmlSetStringCallbacks()' - Set the string copy/free callback functions.
|
|
|
|
|
//
|
|
|
|
|
// This function sets the string copy/free callback functions for the current
|
|
|
|
|
// thread. The `strcopy_cb` function makes a copy of the provided string while
|
|
|
|
|
// the `strfree_cb` function frees the copy. Each callback accepts the
|
|
|
|
|
// `str_cbdata` pointer along with the pointer to the string:
|
|
|
|
|
//
|
|
|
|
|
// ```c
|
|
|
|
|
// char *my_strcopy_cb(void *cbdata, const char *s)
|
|
|
|
|
// {
|
|
|
|
|
// ... make a copy of "s" ...
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void my_strfree_cb(void *cbdata, char *s)
|
|
|
|
|
// {
|
|
|
|
|
// ... release the memory used by "s" ...
|
|
|
|
|
// }
|
|
|
|
|
// ```
|
|
|
|
|
//
|
|
|
|
|
// The default `strcopy_cb` function calls `strdup` while the default
|
|
|
|
|
// `strfree_cb` function calls `free`.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
mxmlSetStringCallbacks(
|
|
|
|
|
mxml_strcopy_cb_t strcopy_cb, // I - String copy callback function
|
|
|
|
|
mxml_strfree_cb_t strfree_cb, // I - String free callback function
|
|
|
|
|
void *str_cbdata) // I - String callback data
|
|
|
|
|
{
|
|
|
|
|
_mxml_global_t *global = _mxml_global();
|
|
|
|
|
// Global data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global->strcopy_cb = strcopy_cb;
|
|
|
|
|
global->strfree_cb = strfree_cb;
|
|
|
|
|
global->str_cbdata = str_cbdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// '_mxml_strcopy()' - Copy a string.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
char * // O - Copy of string
|
|
|
|
|
_mxml_strcopy(const char *s) // I - String
|
|
|
|
|
{
|
|
|
|
|
_mxml_global_t *global = _mxml_global();
|
|
|
|
|
// Global data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
|
|
if (global->strcopy_cb)
|
|
|
|
|
return ((global->strcopy_cb)(global->str_cbdata, s));
|
|
|
|
|
else
|
|
|
|
|
return (strdup(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// '_mxml_strfree()' - Free a string.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_mxml_strfree(char *s) // I - String
|
|
|
|
|
{
|
|
|
|
|
_mxml_global_t *global = _mxml_global();
|
|
|
|
|
// Global data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (global->strfree_cb)
|
|
|
|
|
(global->strfree_cb)(global->str_cbdata, s);
|
|
|
|
|
else
|
|
|
|
|
free((void *)s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#ifdef HAVE_PTHREAD_H // POSIX threading
|
2007-09-22 21:00:56 +00:00
|
|
|
|
# include <pthread.h>
|
|
|
|
|
|
2022-07-14 13:52:34 +00:00
|
|
|
|
static int _mxml_initialized = 0;
|
2024-02-27 20:04:27 +00:00
|
|
|
|
// Have we been initialized?
|
|
|
|
|
static pthread_key_t _mxml_key; // Thread local storage key
|
2007-09-22 21:00:56 +00:00
|
|
|
|
static pthread_once_t _mxml_key_once = PTHREAD_ONCE_INIT;
|
2024-02-27 20:04:27 +00:00
|
|
|
|
// One-time initialization object
|
2007-09-22 21:00:56 +00:00
|
|
|
|
static void _mxml_init(void);
|
|
|
|
|
static void _mxml_destructor(void *g);
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// '_mxml_destructor()' - Free memory used for globals...
|
|
|
|
|
//
|
2010-09-19 04:56:51 +00:00
|
|
|
|
|
|
|
|
|
static void
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_destructor(void *g) // I - Global data
|
2010-09-19 04:56:51 +00:00
|
|
|
|
{
|
|
|
|
|
free(g);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// '_mxml_fini()' - Clean up when unloaded.
|
|
|
|
|
//
|
2010-09-19 04:56:51 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_MXML_FINI(void)
|
|
|
|
|
{
|
2022-07-14 13:52:34 +00:00
|
|
|
|
if (_mxml_initialized)
|
|
|
|
|
pthread_key_delete(_mxml_key);
|
2010-09-19 04:56:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// '_mxml_global()' - Get global data.
|
|
|
|
|
//
|
2007-09-22 21:00:56 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t * // O - Global data
|
2007-09-22 21:00:56 +00:00
|
|
|
|
_mxml_global(void)
|
|
|
|
|
{
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t *global; // Global data
|
2007-09-22 21:00:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pthread_once(&_mxml_key_once, _mxml_init);
|
|
|
|
|
|
|
|
|
|
if ((global = (_mxml_global_t *)pthread_getspecific(_mxml_key)) == NULL)
|
|
|
|
|
{
|
|
|
|
|
global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
|
|
|
|
|
pthread_setspecific(_mxml_key, global);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (global);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// '_mxml_init()' - Initialize global data...
|
|
|
|
|
//
|
2007-09-22 21:00:56 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_mxml_init(void)
|
|
|
|
|
{
|
2022-07-14 13:52:34 +00:00
|
|
|
|
_mxml_initialized = 1;
|
2007-09-22 21:00:56 +00:00
|
|
|
|
pthread_key_create(&_mxml_key, _mxml_destructor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#elif defined(_WIN32) && defined(MXML1_EXPORTS) // WIN32 threading
|
2007-11-22 18:01:52 +00:00
|
|
|
|
# include <windows.h>
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
static DWORD _mxml_tls_index; // Index for global storage
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// 'DllMain()' - Main entry for library.
|
|
|
|
|
//
|
2014-01-04 21:50:06 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
BOOL WINAPI // O - Success/failure
|
|
|
|
|
DllMain(HINSTANCE hinst, // I - DLL module handle
|
|
|
|
|
DWORD reason, // I - Reason
|
|
|
|
|
LPVOID reserved) // I - Unused
|
2007-11-22 18:01:52 +00:00
|
|
|
|
{
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t *global; // Global data
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(void)hinst;
|
|
|
|
|
(void)reserved;
|
|
|
|
|
|
2014-01-04 21:50:06 +00:00
|
|
|
|
switch (reason)
|
|
|
|
|
{
|
2024-02-27 20:04:27 +00:00
|
|
|
|
case DLL_PROCESS_ATTACH : // Called on library initialization
|
2014-01-04 21:50:06 +00:00
|
|
|
|
if ((_mxml_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES)
|
|
|
|
|
return (FALSE);
|
|
|
|
|
break;
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
case DLL_THREAD_DETACH : // Called when a thread terminates
|
2007-11-22 18:01:52 +00:00
|
|
|
|
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
|
|
|
|
|
free(global);
|
2014-01-04 21:50:06 +00:00
|
|
|
|
break;
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
case DLL_PROCESS_DETACH : // Called when library is unloaded
|
2007-11-22 18:01:52 +00:00
|
|
|
|
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
|
|
|
|
|
free(global);
|
|
|
|
|
|
2014-01-04 21:50:06 +00:00
|
|
|
|
TlsFree(_mxml_tls_index);
|
|
|
|
|
break;
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
2014-01-04 21:50:06 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
|
|
|
|
return (TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
//
|
|
|
|
|
// '_mxml_global()' - Get global data.
|
|
|
|
|
//
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t * // O - Global data
|
2007-11-22 18:01:52 +00:00
|
|
|
|
_mxml_global(void)
|
|
|
|
|
{
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t *global; // Global data
|
2007-11-22 18:01:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) == NULL)
|
|
|
|
|
{
|
|
|
|
|
global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
|
|
|
|
|
|
2014-01-04 21:50:06 +00:00
|
|
|
|
TlsSetValue(_mxml_tls_index, (LPVOID)global);
|
2007-11-22 18:01:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (global);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#else // No threading
|
|
|
|
|
//
|
|
|
|
|
// '_mxml_global()' - Get global data.
|
|
|
|
|
//
|
2007-09-22 21:00:56 +00:00
|
|
|
|
|
2024-02-27 20:04:27 +00:00
|
|
|
|
_mxml_global_t * // O - Global data
|
2007-09-22 21:00:56 +00:00
|
|
|
|
_mxml_global(void)
|
|
|
|
|
{
|
2024-02-27 20:04:27 +00:00
|
|
|
|
static _mxml_global_t global = // Global data
|
2007-09-22 21:00:56 +00:00
|
|
|
|
{
|
2024-03-19 01:46:14 +00:00
|
|
|
|
NULL, // strcopy_cb
|
|
|
|
|
NULL, // strfree_cb
|
|
|
|
|
NULL, // str_cbdata
|
2007-09-22 21:00:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (&global);
|
|
|
|
|
}
|
2024-02-27 20:04:27 +00:00
|
|
|
|
#endif // HAVE_PTHREAD_H
|