Blame lib/iconv_open.c

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