Blame iconv/gconv_cache.c

Packit 6c4009
/* Cache handling for iconv modules.
Packit 6c4009
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2001.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
#include <gconv_int.h>
Packit 6c4009
#include <iconvconfig.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
#include "../intl/hash-string.h"
Packit 6c4009
Packit 6c4009
static void *gconv_cache;
Packit 6c4009
static size_t cache_size;
Packit 6c4009
static int cache_malloced;
Packit 6c4009
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
__gconv_get_cache (void)
Packit 6c4009
{
Packit 6c4009
  return gconv_cache;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__gconv_load_cache (void)
Packit 6c4009
{
Packit 6c4009
  int fd;
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  struct gconvcache_header *header;
Packit 6c4009
Packit 6c4009
  /* We cannot use the cache if the GCONV_PATH environment variable is
Packit 6c4009
     set.  */
Packit 6c4009
  __gconv_path_envvar = getenv ("GCONV_PATH");
Packit 6c4009
  if (__gconv_path_envvar != NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* See whether the cache file exists.  */
Packit 6c4009
  fd = __open_nocancel (GCONV_MODULES_CACHE, O_RDONLY, 0);
Packit 6c4009
  if (__builtin_expect (fd, 0) == -1)
Packit 6c4009
    /* Not available.  */
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Get information about the file.  */
Packit 6c4009
  if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0
Packit 6c4009
      /* We do not have to start looking at the file if it cannot contain
Packit 6c4009
	 at least the cache header.  */
Packit 6c4009
      || (size_t) st.st_size < sizeof (struct gconvcache_header))
Packit 6c4009
    {
Packit 6c4009
    close_and_exit:
Packit 6c4009
      __close_nocancel_nostatus (fd);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make the file content available.  */
Packit 6c4009
  cache_size = st.st_size;
Packit 6c4009
#ifdef _POSIX_MAPPED_FILES
Packit 6c4009
  gconv_cache = __mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0);
Packit 6c4009
  if (__glibc_unlikely (gconv_cache == MAP_FAILED))
Packit 6c4009
#endif
Packit 6c4009
    {
Packit 6c4009
      size_t already_read;
Packit 6c4009
Packit 6c4009
      gconv_cache = malloc (cache_size);
Packit 6c4009
      if (gconv_cache == NULL)
Packit 6c4009
	goto close_and_exit;
Packit 6c4009
Packit 6c4009
      already_read = 0;
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  ssize_t n = __read (fd, (char *) gconv_cache + already_read,
Packit 6c4009
			      cache_size - already_read);
Packit 6c4009
	  if (__builtin_expect (n, 0) == -1)
Packit 6c4009
	    {
Packit 6c4009
	      free (gconv_cache);
Packit 6c4009
	      gconv_cache = NULL;
Packit 6c4009
	      goto close_and_exit;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  already_read += n;
Packit 6c4009
	}
Packit 6c4009
      while (already_read < cache_size);
Packit 6c4009
Packit 6c4009
      cache_malloced = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We don't need the file descriptor anymore.  */
Packit 6c4009
  __close_nocancel_nostatus (fd);
Packit 6c4009
Packit 6c4009
  /* Check the consistency.  */
Packit 6c4009
  header = (struct gconvcache_header *) gconv_cache;
Packit 6c4009
  if (__builtin_expect (header->magic, GCONVCACHE_MAGIC) != GCONVCACHE_MAGIC
Packit 6c4009
      || __builtin_expect (header->string_offset >= cache_size, 0)
Packit 6c4009
      || __builtin_expect (header->hash_offset >= cache_size, 0)
Packit 6c4009
      || __builtin_expect (header->hash_size == 0, 0)
Packit 6c4009
      || __builtin_expect ((header->hash_offset
Packit 6c4009
			    + header->hash_size * sizeof (struct hash_entry))
Packit 6c4009
			   > cache_size, 0)
Packit 6c4009
      || __builtin_expect (header->module_offset >= cache_size, 0)
Packit 6c4009
      || __builtin_expect (header->otherconv_offset > cache_size, 0))
Packit 6c4009
    {
Packit 6c4009
      if (cache_malloced)
Packit 6c4009
	{
Packit 6c4009
	  free (gconv_cache);
Packit 6c4009
	  cache_malloced = 0;
Packit 6c4009
	}
Packit 6c4009
#ifdef _POSIX_MAPPED_FILES
Packit 6c4009
      else
Packit 6c4009
	__munmap (gconv_cache, cache_size);
Packit 6c4009
#endif
Packit 6c4009
      gconv_cache = NULL;
Packit 6c4009
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* That worked.  */
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
find_module_idx (const char *str, size_t *idxp)
Packit 6c4009
{
Packit 6c4009
  unsigned int idx;
Packit 6c4009
  unsigned int hval;
Packit 6c4009
  unsigned int hval2;
Packit 6c4009
  const struct gconvcache_header *header;
Packit 6c4009
  const char *strtab;
Packit 6c4009
  const struct hash_entry *hashtab;
Packit 6c4009
  unsigned int limit;
Packit 6c4009
Packit 6c4009
  header = (const struct gconvcache_header *) gconv_cache;
Packit 6c4009
  strtab = (char *) gconv_cache + header->string_offset;
Packit 6c4009
  hashtab = (struct hash_entry *) ((char *) gconv_cache
Packit 6c4009
				   + header->hash_offset);
Packit 6c4009
Packit 6c4009
  hval = __hash_string (str);
Packit 6c4009
  idx = hval % header->hash_size;
Packit 6c4009
  hval2 = 1 + hval % (header->hash_size - 2);
Packit 6c4009
Packit 6c4009
  limit = cache_size - header->string_offset;
Packit 6c4009
  while (hashtab[idx].string_offset != 0)
Packit 6c4009
    if (hashtab[idx].string_offset < limit
Packit 6c4009
	&& strcmp (str, strtab + hashtab[idx].string_offset) == 0)
Packit 6c4009
      {
Packit 6c4009
	*idxp = hashtab[idx].module_idx;
Packit 6c4009
	return 0;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      if ((idx += hval2) >= header->hash_size)
Packit 6c4009
	idx -= header->hash_size;
Packit 6c4009
Packit 6c4009
  /* Nothing found.  */
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifndef STATIC_GCONV
Packit 6c4009
static int
Packit 6c4009
find_module (const char *directory, const char *filename,
Packit 6c4009
	     struct __gconv_step *result)
Packit 6c4009
{
Packit 6c4009
  size_t dirlen = strlen (directory);
Packit 6c4009
  size_t fnamelen = strlen (filename) + 1;
Packit 6c4009
  char fullname[dirlen + fnamelen];
Packit 6c4009
  int status = __GCONV_NOCONV;
Packit 6c4009
Packit 6c4009
  memcpy (__mempcpy (fullname, directory, dirlen), filename, fnamelen);
Packit 6c4009
Packit 6c4009
  result->__shlib_handle = __gconv_find_shlib (fullname);
Packit 6c4009
  if (result->__shlib_handle != NULL)
Packit 6c4009
    {
Packit 6c4009
      status = __GCONV_OK;
Packit 6c4009
Packit 6c4009
      result->__modname = NULL;
Packit 6c4009
      result->__fct = result->__shlib_handle->fct;
Packit 6c4009
      result->__init_fct = result->__shlib_handle->init_fct;
Packit 6c4009
      result->__end_fct = result->__shlib_handle->end_fct;
Packit 6c4009
Packit 6c4009
      /* These settings can be overridden by the init function.  */
Packit 6c4009
      result->__btowc_fct = NULL;
Packit 6c4009
      result->__data = NULL;
Packit 6c4009
Packit 6c4009
      /* Call the init function.  */
Packit 6c4009
      __gconv_init_fct init_fct = result->__init_fct;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
      PTR_DEMANGLE (init_fct);
Packit 6c4009
#endif
Packit 6c4009
      if (init_fct != NULL)
Packit 6c4009
	{
Packit 6c4009
	  status = DL_CALL_FCT (init_fct, (result));
Packit 6c4009
Packit 6c4009
#ifdef PTR_MANGLE
Packit 6c4009
	  PTR_MANGLE (result->__btowc_fct);
Packit 6c4009
#endif
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
Packit 6c4009
{
Packit 6c4009
  size_t name1_idx;
Packit 6c4009
  size_t name2_idx;
Packit 6c4009
Packit 6c4009
  if (gconv_cache == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  if (find_module_idx (name1, &name1_idx) != 0
Packit 6c4009
      || find_module_idx (name2, &name2_idx) != 0)
Packit 6c4009
    *result = strcmp (name1, name2);
Packit 6c4009
  else
Packit 6c4009
    *result = (int) (name1_idx - name2_idx);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__gconv_lookup_cache (const char *toset, const char *fromset,
Packit 6c4009
		      struct __gconv_step **handle, size_t *nsteps, int flags)
Packit 6c4009
{
Packit 6c4009
  const struct gconvcache_header *header;
Packit 6c4009
  const char *strtab;
Packit 6c4009
  size_t fromidx;
Packit 6c4009
  size_t toidx;
Packit 6c4009
  const struct module_entry *modtab;
Packit 6c4009
  const struct module_entry *from_module;
Packit 6c4009
  const struct module_entry *to_module;
Packit 6c4009
  struct __gconv_step *result;
Packit 6c4009
Packit 6c4009
  if (gconv_cache == NULL)
Packit 6c4009
    /* We have no cache available.  */
Packit 6c4009
    return __GCONV_NODB;
Packit 6c4009
Packit 6c4009
  header = (const struct gconvcache_header *) gconv_cache;
Packit 6c4009
  strtab = (char *) gconv_cache + header->string_offset;
Packit 6c4009
  modtab = (const struct module_entry *) ((char *) gconv_cache
Packit 6c4009
					  + header->module_offset);
Packit 6c4009
Packit 6c4009
  if (find_module_idx (fromset, &fromidx) != 0
Packit 6c4009
      || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
Packit 6c4009
	  > cache_size))
Packit 6c4009
    return __GCONV_NOCONV;
Packit 6c4009
  from_module = &modtab[fromidx];
Packit 6c4009
Packit 6c4009
  if (find_module_idx (toset, &toidx) != 0
Packit 6c4009
      || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
Packit 6c4009
	  > cache_size))
Packit 6c4009
    return __GCONV_NOCONV;
Packit 6c4009
  to_module = &modtab[toidx];
Packit 6c4009
Packit 6c4009
  /* Avoid copy-only transformations if the user requests.   */
Packit 6c4009
  if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
Packit 6c4009
    return __GCONV_NULCONV;
Packit 6c4009
Packit 6c4009
  /* If there are special conversions available examine them first.  */
Packit 6c4009
  if (fromidx != 0 && toidx != 0
Packit 6c4009
      && __builtin_expect (from_module->extra_offset, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      /* Search through the list to see whether there is a module
Packit 6c4009
	 matching the destination character set.  */
Packit 6c4009
      const struct extra_entry *extra;
Packit 6c4009
Packit 6c4009
      /* Note the -1.  This is due to the offset added in iconvconfig.
Packit 6c4009
	 See there for more explanations.  */
Packit 6c4009
      extra = (const struct extra_entry *) ((char *) gconv_cache
Packit 6c4009
					    + header->otherconv_offset
Packit 6c4009
					    + from_module->extra_offset - 1);
Packit 6c4009
      while (extra->module_cnt != 0
Packit 6c4009
	     && extra->module[extra->module_cnt - 1].outname_offset != toidx)
Packit 6c4009
	extra = (const struct extra_entry *) ((char *) extra
Packit 6c4009
					      + sizeof (struct extra_entry)
Packit 6c4009
					      + (extra->module_cnt
Packit 6c4009
						 * sizeof (struct extra_entry_module)));
Packit 6c4009
Packit 6c4009
      if (extra->module_cnt != 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Use the extra module.  First determine how many steps.  */
Packit 6c4009
	  char *fromname;
Packit 6c4009
	  int idx;
Packit 6c4009
Packit 6c4009
	  *nsteps = extra->module_cnt;
Packit 6c4009
	  *handle = result =
Packit 6c4009
	    (struct __gconv_step *) malloc (extra->module_cnt
Packit 6c4009
					    * sizeof (struct __gconv_step));
Packit 6c4009
	  if (result == NULL)
Packit 6c4009
	    return __GCONV_NOMEM;
Packit 6c4009
Packit 6c4009
	  fromname = (char *) strtab + from_module->canonname_offset;
Packit 6c4009
	  idx = 0;
Packit 6c4009
	  do
Packit 6c4009
	    {
Packit 6c4009
	      result[idx].__from_name = fromname;
Packit 6c4009
	      fromname = result[idx].__to_name =
Packit 6c4009
		(char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
Packit 6c4009
Packit 6c4009
	      result[idx].__counter = 1;
Packit 6c4009
	      result[idx].__data = NULL;
Packit 6c4009
Packit 6c4009
#ifndef STATIC_GCONV
Packit 6c4009
	      if (strtab[extra->module[idx].dir_offset] != '\0')
Packit 6c4009
		{
Packit 6c4009
		  /* Load the module, return handle for it.  */
Packit 6c4009
		  int res;
Packit 6c4009
Packit 6c4009
		  res = find_module (strtab + extra->module[idx].dir_offset,
Packit 6c4009
				     strtab + extra->module[idx].name_offset,
Packit 6c4009
				     &result[idx]);
Packit 6c4009
		  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
Packit 6c4009
		    {
Packit 6c4009
		      /* Something went wrong.  */
Packit 6c4009
		      free (result);
Packit 6c4009
		      goto try_internal;
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
#endif
Packit 6c4009
		/* It's a builtin transformation.  */
Packit 6c4009
		__gconv_get_builtin_trans (strtab
Packit 6c4009
					   + extra->module[idx].name_offset,
Packit 6c4009
					   &result[idx]);
Packit 6c4009
Packit 6c4009
	    }
Packit 6c4009
	  while (++idx < extra->module_cnt);
Packit 6c4009
Packit 6c4009
	  return __GCONV_OK;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 try_internal:
Packit 6c4009
  /* See whether we can convert via the INTERNAL charset.  */
Packit 6c4009
  if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
Packit 6c4009
      || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
Packit 6c4009
      || (fromidx == 0 && toidx == 0))
Packit 6c4009
    /* Not possible.  Nothing we can do.  */
Packit 6c4009
    return __GCONV_NOCONV;
Packit 6c4009
Packit 6c4009
  /* We will use up to two modules.  Always allocate room for two.  */
Packit 6c4009
  result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    return __GCONV_NOMEM;
Packit 6c4009
Packit 6c4009
  *handle = result;
Packit 6c4009
  *nsteps = 0;
Packit 6c4009
Packit 6c4009
  /* Generate data structure for conversion to INTERNAL.  */
Packit 6c4009
  if (fromidx != 0)
Packit 6c4009
    {
Packit 6c4009
      result[0].__from_name = (char *) strtab + from_module->canonname_offset;
Packit 6c4009
      result[0].__to_name = (char *) "INTERNAL";
Packit 6c4009
Packit 6c4009
      result[0].__counter = 1;
Packit 6c4009
      result[0].__data = NULL;
Packit 6c4009
Packit 6c4009
#ifndef STATIC_GCONV
Packit 6c4009
      if (strtab[from_module->todir_offset] != '\0')
Packit 6c4009
	{
Packit 6c4009
	  /* Load the module, return handle for it.  */
Packit 6c4009
	  int res = find_module (strtab + from_module->todir_offset,
Packit 6c4009
				 strtab + from_module->toname_offset,
Packit 6c4009
				 &result[0]);
Packit 6c4009
	  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
Packit 6c4009
	    {
Packit 6c4009
	      /* Something went wrong.  */
Packit 6c4009
	      free (result);
Packit 6c4009
	      return res;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	/* It's a builtin transformation.  */
Packit 6c4009
	__gconv_get_builtin_trans (strtab + from_module->toname_offset,
Packit 6c4009
				   &result[0]);
Packit 6c4009
Packit 6c4009
      ++*nsteps;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Generate data structure for conversion from INTERNAL.  */
Packit 6c4009
  if (toidx != 0)
Packit 6c4009
    {
Packit 6c4009
      int idx = *nsteps;
Packit 6c4009
Packit 6c4009
      result[idx].__from_name = (char *) "INTERNAL";
Packit 6c4009
      result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
Packit 6c4009
Packit 6c4009
      result[idx].__counter = 1;
Packit 6c4009
      result[idx].__data = NULL;
Packit 6c4009
Packit 6c4009
#ifndef STATIC_GCONV
Packit 6c4009
      if (strtab[to_module->fromdir_offset] != '\0')
Packit 6c4009
	{
Packit 6c4009
	  /* Load the module, return handle for it.  */
Packit 6c4009
	  int res = find_module (strtab + to_module->fromdir_offset,
Packit 6c4009
				 strtab + to_module->fromname_offset,
Packit 6c4009
				 &result[idx]);
Packit 6c4009
	  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
Packit 6c4009
	    {
Packit 6c4009
	      /* Something went wrong.  */
Packit 6c4009
	      if (idx != 0)
Packit 6c4009
		__gconv_release_step (&result[0]);
Packit 6c4009
	      free (result);
Packit 6c4009
	      return res;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	/* It's a builtin transformation.  */
Packit 6c4009
	__gconv_get_builtin_trans (strtab + to_module->fromname_offset,
Packit 6c4009
				   &result[idx]);
Packit 6c4009
Packit 6c4009
      ++*nsteps;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return __GCONV_OK;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Free memory allocated for the transformation record.  */
Packit 6c4009
void
Packit 6c4009
__gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
Packit 6c4009
{
Packit 6c4009
  if (gconv_cache != NULL)
Packit 6c4009
    /* The only thing we have to deallocate is the record with the
Packit 6c4009
       steps.  */
Packit 6c4009
    free (steps);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Free all resources if necessary.  */
Packit 6c4009
libc_freeres_fn (free_mem)
Packit 6c4009
{
Packit 6c4009
  if (cache_malloced)
Packit 6c4009
    free (gconv_cache);
Packit 6c4009
#ifdef _POSIX_MAPPED_FILES
Packit 6c4009
  else if (gconv_cache != NULL)
Packit 6c4009
    __munmap (gconv_cache, cache_size);
Packit 6c4009
#endif
Packit 6c4009
}