/* Provide access to the collection of available transformation modules.
- Copyright (C) 1997,98,99,2000,2001 Free Software Foundation, Inc.
+ Copyright (C) 1997-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU Library General Public
- License along with the GNU C Library; see the file COPYING.LIB. If not,
- write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+#include <assert.h>
#include <limits.h>
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
+#include <locale/localeinfo.h>
#include <dlfcn.h>
#include <gconv_int.h>
-#include <gconv_charset.h>
+#include <sysdep.h>
/* Simple data structure for alias mapping. We have two names, `from'
struct gconv_module *__gconv_modules_db;
/* We modify global data. */
-__libc_lock_define_initialized (static, lock)
+__libc_lock_define_initialized (, __gconv_lock)
+
+
+/* Provide access to module database. */
+struct gconv_module *
+__gconv_get_modules_db (void)
+{
+ return __gconv_modules_db;
+}
+
+void *
+__gconv_get_alias_db (void)
+{
+ return __gconv_alias_db;
+}
/* Function for searching alias. */
/* Look up whether given transformation was already requested before. */
static int
-internal_function
derivation_lookup (const char *fromset, const char *toset,
struct __gconv_step **handle, size_t *nsteps)
{
/* Add new derivation to list of known ones. */
static void
-internal_function
add_derivation (const char *fromset, const char *toset,
struct __gconv_step *handle, size_t nsteps)
{
not all memory will be freed. */
}
-static void
+static void __libc_freeres_fn_section
free_derivation (void *p)
{
struct known_derivation *deriv = (struct known_derivation *) p;
for (cnt = 0; cnt < deriv->nsteps; ++cnt)
if (deriv->steps[cnt].__counter > 0
- && deriv->steps[cnt].__end_fct != NULL)
- DL_CALL_FCT (deriv->steps[cnt].__end_fct, (&deriv->steps[cnt]));
+ && deriv->steps[cnt].__shlib_handle != NULL)
+ {
+ __gconv_end_fct end_fct = deriv->steps[cnt].__end_fct;
+#ifdef PTR_DEMANGLE
+ PTR_DEMANGLE (end_fct);
+#endif
+ if (end_fct != NULL)
+ DL_CALL_FCT (end_fct, (&deriv->steps[cnt]));
+ }
/* Free the name strings. */
- free ((char *) deriv->steps[0].__from_name);
- free ((char *) deriv->steps[deriv->nsteps - 1].__to_name);
+ if (deriv->steps != NULL)
+ {
+ free ((char *) deriv->steps[0].__from_name);
+ free ((char *) deriv->steps[deriv->nsteps - 1].__to_name);
+ free ((struct __gconv_step *) deriv->steps);
+ }
- free ((struct __gconv_step *) deriv->steps);
free (deriv);
}
/* Decrement the reference count for a single step in a steps array. */
-static inline void
-release_step (struct __gconv_step *step)
+void
+__gconv_release_step (struct __gconv_step *step)
{
- if (--step->__counter == 0)
+ /* Skip builtin modules; they are not reference counted. */
+ if (step->__shlib_handle != NULL && --step->__counter == 0)
{
/* Call the destructor. */
- if (step->__end_fct != NULL)
- DL_CALL_FCT (step->__end_fct, (step));
+ __gconv_end_fct end_fct = step->__end_fct;
+#ifdef PTR_DEMANGLE
+ PTR_DEMANGLE (end_fct);
+#endif
+ if (end_fct != NULL)
+ DL_CALL_FCT (end_fct, (step));
#ifndef STATIC_GCONV
- /* Skip builtin modules; they are not reference counted. */
- if (step->__shlib_handle != NULL)
- {
- /* Release the loaded module. */
- __gconv_release_shlib (step->__shlib_handle);
- step->__shlib_handle = NULL;
- }
+ /* Release the loaded module. */
+ __gconv_release_shlib (step->__shlib_handle);
+ step->__shlib_handle = NULL;
#endif
}
+ else if (step->__shlib_handle == NULL)
+ /* Builtin modules should not have end functions. */
+ assert (step->__end_fct == NULL);
}
static int
-internal_function
gen_steps (struct derivation_step *best, const char *toset,
const char *fromset, struct __gconv_step **handle, size_t *nsteps)
{
struct __gconv_step *result;
struct derivation_step *current;
int status = __GCONV_NOMEM;
+ char *from_name = NULL;
+ char *to_name = NULL;
/* First determine number of steps. */
for (current = best; current->last != NULL; current = current->last)
current = best;
while (step_cnt-- > 0)
{
- result[step_cnt].__from_name = (step_cnt == 0
- ? __strdup (fromset)
- : (char *)current->last->result_set);
- result[step_cnt].__to_name = (step_cnt + 1 == *nsteps
- ? __strdup (current->result_set)
- : result[step_cnt + 1].__from_name);
+ if (step_cnt == 0)
+ {
+ result[step_cnt].__from_name = from_name = __strdup (fromset);
+ if (from_name == NULL)
+ {
+ failed = 1;
+ break;
+ }
+ }
+ else
+ result[step_cnt].__from_name = (char *)current->last->result_set;
+
+ if (step_cnt + 1 == *nsteps)
+ {
+ result[step_cnt].__to_name = to_name
+ = __strdup (current->result_set);
+ if (to_name == NULL)
+ {
+ failed = 1;
+ break;
+ }
+ }
+ else
+ result[step_cnt].__to_name = result[step_cnt + 1].__from_name;
+
+ result[step_cnt].__counter = 1;
+ result[step_cnt].__data = NULL;
#ifndef STATIC_GCONV
if (current->code->module_name[0] == '/')
result[step_cnt].__fct = shlib_handle->fct;
result[step_cnt].__init_fct = shlib_handle->init_fct;
result[step_cnt].__end_fct = shlib_handle->end_fct;
+
+ /* These settings can be overridden by the init function. */
+ result[step_cnt].__btowc_fct = NULL;
+
+ /* Call the init function. */
+ __gconv_init_fct init_fct = result[step_cnt].__init_fct;
+# ifdef PTR_DEMANGLE
+ PTR_DEMANGLE (init_fct);
+# endif
+ if (init_fct != NULL)
+ {
+ status = DL_CALL_FCT (init_fct, (&result[step_cnt]));
+
+ if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
+ {
+ failed = 1;
+ /* Do not call the end function because the init
+ function has failed. */
+ result[step_cnt].__end_fct = NULL;
+# ifdef PTR_MANGLE
+ PTR_MANGLE (result[step_cnt].__end_fct);
+# endif
+ /* Make sure we unload this module. */
+ --step_cnt;
+ break;
+ }
+ }
+# ifdef PTR_MANGLE
+ PTR_MANGLE (result[step_cnt].__btowc_fct);
+# endif
}
else
#endif
__gconv_get_builtin_trans (current->code->module_name,
&result[step_cnt]);
- result[step_cnt].__counter = 1;
-
- /* Call the init function. */
- result[step_cnt].__data = NULL;
- if (result[step_cnt].__init_fct != NULL)
- {
- status = DL_CALL_FCT (result[step_cnt].__init_fct,
- (&result[step_cnt]));
-
- if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
- {
- failed = 1;
- /* Make sure we unload this modules. */
- --step_cnt;
- result[step_cnt].__end_fct = NULL;
- break;
- }
- }
-
current = current->last;
}
{
/* Something went wrong while initializing the modules. */
while (++step_cnt < *nsteps)
- release_step (&result[step_cnt]);
+ __gconv_release_step (&result[step_cnt]);
free (result);
+ free (from_name);
+ free (to_name);
*nsteps = 0;
*handle = NULL;
if (status == __GCONV_OK)
#ifndef STATIC_GCONV
static int
-internal_function
increment_counter (struct __gconv_step *steps, size_t nsteps)
{
/* Increment the user counter. */
(after unloading) and this time loading failed!? */
--step->__counter;
while (++cnt < nsteps)
- release_step (&steps[cnt]);
+ __gconv_release_step (&steps[cnt]);
result = __GCONV_NOCONV;
break;
}
step->__fct = step->__shlib_handle->fct;
step->__init_fct = step->__shlib_handle->init_fct;
step->__end_fct = step->__shlib_handle->end_fct;
- }
- if (step->__init_fct != NULL)
- DL_CALL_FCT (step->__init_fct, (step));
+ /* These settings can be overridden by the init function. */
+ step->__btowc_fct = NULL;
+
+ /* Call the init function. */
+ __gconv_init_fct init_fct = step->__init_fct;
+#ifdef PTR_DEMANGLE
+ PTR_DEMANGLE (init_fct);
+#endif
+ if (init_fct != NULL)
+ DL_CALL_FCT (init_fct, (step));
+
+#ifdef PTR_MANGLE
+ PTR_MANGLE (step->__btowc_fct);
+#endif
+ }
}
}
return result;
/* The main function: find a possible derivation from the `fromset' (either
the given name or the alias) to the `toset' (again with alias). */
static int
-internal_function
find_derivation (const char *toset, const char *toset_expand,
const char *fromset, const char *fromset_expand,
struct __gconv_step **handle, size_t *nsteps)
}
-/* Control of initialization. */
-__libc_once_define (static, once);
-
-
static const char *
do_lookup_alias (const char *name)
{
}
-const char *
-__gconv_lookup_alias (const char *name)
+int
+__gconv_compare_alias (const char *name1, const char *name2)
{
+ int result;
+
/* Ensure that the configuration data is read. */
- __libc_once (once, __gconv_read_conf);
+ __gconv_load_conf ();
- return do_lookup_alias (name) ?: name;
+ if (__gconv_compare_alias_cache (name1, name2, &result) != 0)
+ result = strcmp (do_lookup_alias (name1) ?: name1,
+ do_lookup_alias (name2) ?: name2);
+
+ return result;
}
int
-internal_function
__gconv_find_transform (const char *toset, const char *fromset,
struct __gconv_step **handle, size_t *nsteps,
int flags)
{
- const char *fromset_expand = NULL;
- const char *toset_expand = NULL;
+ const char *fromset_expand;
+ const char *toset_expand;
int result;
/* Ensure that the configuration data is read. */
- __libc_once (once, __gconv_read_conf);
+ __gconv_load_conf ();
/* Acquire the lock. */
- __libc_lock_lock (lock);
+ __libc_lock_lock (__gconv_lock);
+
+ result = __gconv_lookup_cache (toset, fromset, handle, nsteps, flags);
+ if (result != __GCONV_NODB)
+ {
+ /* We have a cache and could resolve the request, successful or not. */
+ __libc_lock_unlock (__gconv_lock);
+ return result;
+ }
/* If we don't have a module database return with an error. */
if (__gconv_modules_db == NULL)
{
- __libc_lock_unlock (lock);
+ __libc_lock_unlock (__gconv_lock);
return __GCONV_NOCONV;
}
/* See whether the names are aliases. */
- if (__gconv_alias_db != NULL)
- {
- fromset_expand = do_lookup_alias (fromset);
- toset_expand = do_lookup_alias (toset);
- }
+ fromset_expand = do_lookup_alias (fromset);
+ toset_expand = do_lookup_alias (toset);
if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0)
/* We are not supposed to create a pseudo transformation (means
&& strcmp (toset_expand, fromset_expand) == 0)))))
{
/* Both character sets are the same. */
- __libc_lock_unlock (lock);
- return __GCONV_NOCONV;
+ __libc_lock_unlock (__gconv_lock);
+ return __GCONV_NULCONV;
}
result = find_derivation (toset, toset_expand, fromset, fromset_expand,
handle, nsteps);
/* Release the lock. */
- __libc_lock_unlock (lock);
+ __libc_lock_unlock (__gconv_lock);
/* The following code is necessary since `find_derivation' will return
GCONV_OK even when no derivation was found but the same request
/* Release the entries of the modules list. */
int
-internal_function
__gconv_close_transform (struct __gconv_step *steps, size_t nsteps)
{
int result = __GCONV_OK;
+ size_t cnt;
-#ifndef STATIC_GCONV
/* Acquire the lock. */
- __libc_lock_lock (lock);
+ __libc_lock_lock (__gconv_lock);
+
+#ifndef STATIC_GCONV
+ cnt = nsteps;
+ while (cnt-- > 0)
+ __gconv_release_step (&steps[cnt]);
+#endif
- while (nsteps-- > 0)
- release_step (&steps[nsteps]);
+ /* If we use the cache we free a bit more since we don't keep any
+ transformation records around, they are cheap enough to
+ recreate. */
+ __gconv_release_cache (steps, nsteps);
/* Release the lock. */
- __libc_lock_unlock (lock);
-#endif
+ __libc_lock_unlock (__gconv_lock);
return result;
}
/* Free the modules mentioned. */
static void
-internal_function
+__libc_freeres_fn_section
free_modules_db (struct gconv_module *node)
{
if (node->left != NULL)
/* Free all resources if necessary. */
-static void __attribute__ ((unused))
-free_mem (void)
+libc_freeres_fn (free_mem)
{
+ /* First free locale memory. This needs to be done before freeing
+ derivations, as ctype cleanup functions dereference steps arrays which we
+ free below. */
+ _nl_locale_subfreeres ();
+
+ /* finddomain.c has similar problem. */
+ extern void _nl_finddomain_subfreeres (void) attribute_hidden;
+ _nl_finddomain_subfreeres ();
+
if (__gconv_alias_db != NULL)
__tdestroy (__gconv_alias_db, free);
if (known_derivations != NULL)
__tdestroy (known_derivations, free_derivation);
}
-
-text_set_element (__libc_subfreeres, free_mem);