]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconv/gconv_db.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / iconv / gconv_db.c
index 92b520987b0b76ea0492abd3c11858073a84cce2..861a142068a44accdabc3c89b2c29de6b7132290 100644 (file)
@@ -1,5 +1,5 @@
 /* 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.
 
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   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'
@@ -38,7 +39,21 @@ void *__gconv_alias_db;
 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.  */
@@ -106,7 +121,6 @@ static void *known_derivations;
 
 /* 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)
 {
@@ -128,7 +142,6 @@ derivation_lookup (const char *fromset, const char *toset,
 
 /* 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)
 {
@@ -157,7 +170,7 @@ add_derivation (const char *fromset, const char *toset,
      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;
@@ -165,43 +178,55 @@ free_derivation (void *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.  */
 void
-internal_function
 __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)
 {
@@ -209,6 +234,8 @@ gen_steps (struct derivation_step *best, const char *toset,
   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)
@@ -225,12 +252,30 @@ gen_steps (struct derivation_step *best, const char *toset,
       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;
@@ -254,21 +299,35 @@ gen_steps (struct derivation_step *best, const char *toset,
              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.  */
-             if (result[step_cnt].__init_fct != NULL)
+             __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 (result[step_cnt].__init_fct,
-                                       (&result[step_cnt]));
+                 status = DL_CALL_FCT (init_fct, (&result[step_cnt]));
 
                  if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
                    {
                      failed = 1;
-                     /* Make sure we unload this modules.  */
-                     --step_cnt;
+                     /* 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
@@ -285,6 +344,8 @@ gen_steps (struct derivation_step *best, const char *toset,
          while (++step_cnt < *nsteps)
            __gconv_release_step (&result[step_cnt]);
          free (result);
+         free (from_name);
+         free (to_name);
          *nsteps = 0;
          *handle = NULL;
          if (status == __GCONV_OK)
@@ -305,7 +366,6 @@ gen_steps (struct derivation_step *best, const char *toset,
 
 #ifndef STATIC_GCONV
 static int
-internal_function
 increment_counter (struct __gconv_step *steps, size_t nsteps)
 {
   /* Increment the user counter.  */
@@ -339,10 +399,22 @@ increment_counter (struct __gconv_step *steps, size_t nsteps)
              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;
@@ -353,7 +425,6 @@ increment_counter (struct __gconv_step *steps, size_t nsteps)
 /* 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)
@@ -616,10 +687,6 @@ find_derivation (const char *toset, const char *toset_expand,
 }
 
 
-/* Control of initialization.  */
-__libc_once_define (static, once);
-
-
 static const char *
 do_lookup_alias (const char *name)
 {
@@ -632,18 +699,23 @@ 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 ();
+
+  if (__gconv_compare_alias_cache (name1, name2, &result) != 0)
+    result = strcmp (do_lookup_alias (name1) ?: name1,
+                    do_lookup_alias (name2) ?: name2);
 
-  return do_lookup_alias (name) ?: name;
+  return result;
 }
 
 
 int
-internal_function
 __gconv_find_transform (const char *toset, const char *fromset,
                        struct __gconv_step **handle, size_t *nsteps,
                        int flags)
@@ -653,23 +725,23 @@ __gconv_find_transform (const char *toset, const char *fromset,
   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 (lock);
+      __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;
     }
 
@@ -688,15 +760,15 @@ __gconv_find_transform (const char *toset, const char *fromset,
                      && 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
@@ -709,21 +781,27 @@ __gconv_find_transform (const char *toset, const char *fromset,
 
 /* 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)
-    __gconv_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;
 }
@@ -731,7 +809,7 @@ __gconv_close_transform (struct __gconv_step *steps, size_t nsteps)
 
 /* Free the modules mentioned.  */
 static void
-internal_function
+__libc_freeres_fn_section
 free_modules_db (struct gconv_module *node)
 {
   if (node->left != NULL)
@@ -750,9 +828,17 @@ free_modules_db (struct gconv_module *node)
 
 
 /* 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);
 
@@ -762,5 +848,3 @@ free_mem (void)
   if (known_derivations != NULL)
     __tdestroy (known_derivations, free_derivation);
 }
-
-text_set_element (__libc_subfreeres, free_mem);