Blame lib/iconv_open.c

Packit Service fdd496
/* Character set conversion.
Packit Service fdd496
   Copyright (C) 2007, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software; you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3, or (at your option)
Packit Service fdd496
   any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License along
Packit Service fdd496
   with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
#include <iconv.h>
Packit Service fdd496
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
#include "c-ctype.h"
Packit Service fdd496
#include "c-strcase.h"
Packit Service fdd496
Packit Service fdd496
#define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
Packit Service fdd496
Packit Service fdd496
/* Namespace cleanliness.  */
Packit Service fdd496
#define mapping_lookup rpl_iconv_open_mapping_lookup
Packit Service fdd496
Packit Service fdd496
/* The macro ICONV_FLAVOR is defined to one of these or undefined.  */
Packit Service fdd496
Packit Service fdd496
#define ICONV_FLAVOR_AIX "iconv_open-aix.h"
Packit Service fdd496
#define ICONV_FLAVOR_HPUX "iconv_open-hpux.h"
Packit Service fdd496
#define ICONV_FLAVOR_IRIX "iconv_open-irix.h"
Packit Service fdd496
#define ICONV_FLAVOR_OSF "iconv_open-osf.h"
Packit Service fdd496
#define ICONV_FLAVOR_SOLARIS "iconv_open-solaris.h"
Packit Service fdd496
Packit Service fdd496
#ifdef ICONV_FLAVOR
Packit Service fdd496
# include ICONV_FLAVOR
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
iconv_t
Packit Service fdd496
rpl_iconv_open (const char *tocode, const char *fromcode)
Packit Service fdd496
#undef iconv_open
Packit Service fdd496
{
Packit Service fdd496
  char fromcode_upper[32];
Packit Service fdd496
  char tocode_upper[32];
Packit Service fdd496
  char *fromcode_upper_end;
Packit Service fdd496
  char *tocode_upper_end;
Packit Service fdd496
Packit Service fdd496
#if REPLACE_ICONV_UTF
Packit Service fdd496
  /* Special handling of conversion between UTF-8 and UTF-{16,32}{BE,LE}.
Packit Service fdd496
     Do this here, before calling the real iconv_open(), because  OSF/1 5.1
Packit Service fdd496
     iconv() to these encoding inserts a BOM, which is wrong.
Packit Service fdd496
     We do not need to handle conversion between arbitrary encodings and
Packit Service fdd496
     UTF-{16,32}{BE,LE}, because the 'striconveh' module implements two-step
Packit Service fdd496
     conversion through UTF-8.
Packit Service fdd496
     The _ICONV_* constants are chosen to be disjoint from any iconv_t
Packit Service fdd496
     returned by the system's iconv_open() functions.  Recall that iconv_t
Packit Service fdd496
     is a scalar type.  */
Packit Service fdd496
  if (c_toupper (fromcode[0]) == 'U'
Packit Service fdd496
      && c_toupper (fromcode[1]) == 'T'
Packit Service fdd496
      && c_toupper (fromcode[2]) == 'F'
Packit Service fdd496
      && fromcode[3] == '-')
Packit Service fdd496
    {
Packit Service fdd496
      if (c_toupper (tocode[0]) == 'U'
Packit Service fdd496
          && c_toupper (tocode[1]) == 'T'
Packit Service fdd496
          && c_toupper (tocode[2]) == 'F'
Packit Service fdd496
          && tocode[3] == '-')
Packit Service fdd496
        {
Packit Service fdd496
          if (strcmp (fromcode + 4, "8") == 0)
Packit Service fdd496
            {
Packit Service fdd496
              if (c_strcasecmp (tocode + 4, "16BE") == 0)
Packit Service fdd496
                return _ICONV_UTF8_UTF16BE;
Packit Service fdd496
              if (c_strcasecmp (tocode + 4, "16LE") == 0)
Packit Service fdd496
                return _ICONV_UTF8_UTF16LE;
Packit Service fdd496
              if (c_strcasecmp (tocode + 4, "32BE") == 0)
Packit Service fdd496
                return _ICONV_UTF8_UTF32BE;
Packit Service fdd496
              if (c_strcasecmp (tocode + 4, "32LE") == 0)
Packit Service fdd496
                return _ICONV_UTF8_UTF32LE;
Packit Service fdd496
            }
Packit Service fdd496
          else if (strcmp (tocode + 4, "8") == 0)
Packit Service fdd496
            {
Packit Service fdd496
              if (c_strcasecmp (fromcode + 4, "16BE") == 0)
Packit Service fdd496
                return _ICONV_UTF16BE_UTF8;
Packit Service fdd496
              if (c_strcasecmp (fromcode + 4, "16LE") == 0)
Packit Service fdd496
                return _ICONV_UTF16LE_UTF8;
Packit Service fdd496
              if (c_strcasecmp (fromcode + 4, "32BE") == 0)
Packit Service fdd496
                return _ICONV_UTF32BE_UTF8;
Packit Service fdd496
              if (c_strcasecmp (fromcode + 4, "32LE") == 0)
Packit Service fdd496
                return _ICONV_UTF32LE_UTF8;
Packit Service fdd496
            }
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  /* Do *not* add special support for 8-bit encodings like ASCII or ISO-8859-1
Packit Service fdd496
     here.  This would lead to programs that work in some locales (such as the
Packit Service fdd496
     "C" or "en_US" locales) but do not work in East Asian locales.  It is
Packit Service fdd496
     better if programmers make their programs depend on GNU libiconv (except
Packit Service fdd496
     on glibc systems), e.g. by using the AM_ICONV macro and documenting the
Packit Service fdd496
     dependency in an INSTALL or DEPENDENCIES file.  */
Packit Service fdd496
Packit Service fdd496
  /* Try with the original names first.
Packit Service fdd496
     This covers the case when fromcode or tocode is a lowercase encoding name
Packit Service fdd496
     that is understood by the system's iconv_open but not listed in our
Packit Service fdd496
     mappings table.  */
Packit Service fdd496
  {
Packit Service fdd496
    iconv_t cd = iconv_open (tocode, fromcode);
Packit Service fdd496
    if (cd != (iconv_t)(-1))
Packit Service fdd496
      return cd;
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Convert the encodings to upper case, because
Packit Service fdd496
       1. in the arguments of iconv_open() on AIX, HP-UX, and OSF/1 the case
Packit Service fdd496
          matters,
Packit Service fdd496
       2. it makes searching in the table faster.  */
Packit Service fdd496
  {
Packit Service fdd496
    const char *p = fromcode;
Packit Service fdd496
    char *q = fromcode_upper;
Packit Service fdd496
    while ((*q = c_toupper (*p)) != '\0')
Packit Service fdd496
      {
Packit Service fdd496
        p++;
Packit Service fdd496
        q++;
Packit Service fdd496
        if (q == &fromcode_upper[SIZEOF (fromcode_upper)])
Packit Service fdd496
          {
Packit Service fdd496
            errno = EINVAL;
Packit Service fdd496
            return (iconv_t)(-1);
Packit Service fdd496
          }
Packit Service fdd496
      }
Packit Service fdd496
    fromcode_upper_end = q;
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  {
Packit Service fdd496
    const char *p = tocode;
Packit Service fdd496
    char *q = tocode_upper;
Packit Service fdd496
    while ((*q = c_toupper (*p)) != '\0')
Packit Service fdd496
      {
Packit Service fdd496
        p++;
Packit Service fdd496
        q++;
Packit Service fdd496
        if (q == &tocode_upper[SIZEOF (tocode_upper)])
Packit Service fdd496
          {
Packit Service fdd496
            errno = EINVAL;
Packit Service fdd496
            return (iconv_t)(-1);
Packit Service fdd496
          }
Packit Service fdd496
      }
Packit Service fdd496
    tocode_upper_end = q;
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
#ifdef ICONV_FLAVOR
Packit Service fdd496
  /* Apply the mappings.  */
Packit Service fdd496
  {
Packit Service fdd496
    const struct mapping *m =
Packit Service fdd496
      mapping_lookup (fromcode_upper, fromcode_upper_end - fromcode_upper);
Packit Service fdd496
Packit Service fdd496
    fromcode = (m != NULL ? m->vendor_name : fromcode_upper);
Packit Service fdd496
  }
Packit Service fdd496
  {
Packit Service fdd496
    const struct mapping *m =
Packit Service fdd496
      mapping_lookup (tocode_upper, tocode_upper_end - tocode_upper);
Packit Service fdd496
Packit Service fdd496
    tocode = (m != NULL ? m->vendor_name : tocode_upper);
Packit Service fdd496
  }
Packit Service fdd496
#else
Packit Service fdd496
  fromcode = fromcode_upper;
Packit Service fdd496
  tocode = tocode_upper;
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  return iconv_open (tocode, fromcode);
Packit Service fdd496
}