Blame lib/iconv_open.c

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