Blame lib/iconv_open.c

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