Blame iconv/iconv_open.c

Packit 6c4009
/* Get descriptor for character set conversion.
Packit 6c4009
   Copyright (C) 1997-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>, 1997.
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 <alloca.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <gconv_int.h>
Packit 6c4009
#include "gconv_charset.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
iconv_t
Packit 6c4009
iconv_open (const char *tocode, const char *fromcode)
Packit 6c4009
{
Packit Service c82647
  /* Normalize the name.  We remove all characters beside alpha-numeric,
Packit Service c82647
     '_', '-', '/', '.', and ':'.  */
Packit Service c82647
  size_t tocode_len = strlen (tocode) + 3;
Packit Service c82647
  char *tocode_conv;
Packit Service c82647
  bool tocode_usealloca = __libc_use_alloca (tocode_len);
Packit Service c82647
  if (tocode_usealloca)
Packit Service c82647
    tocode_conv = (char *) alloca (tocode_len);
Packit Service c82647
  else
Packit Service c82647
    {
Packit Service c82647
      tocode_conv = (char *) malloc (tocode_len);
Packit Service c82647
      if (tocode_conv == NULL)
Packit Service c82647
	return (iconv_t) -1;
Packit Service c82647
    }
Packit Service c82647
  strip (tocode_conv, tocode);
Packit Service c82647
  tocode = (tocode_conv[2] == '\0' && tocode[0] != '\0'
Packit Service c82647
	    ? upstr (tocode_conv, tocode) : tocode_conv);
Packit 6c4009
Packit Service c82647
  size_t fromcode_len = strlen (fromcode) + 3;
Packit Service c82647
  char *fromcode_conv;
Packit Service c82647
  bool fromcode_usealloca = __libc_use_alloca (fromcode_len);
Packit Service c82647
  if (fromcode_usealloca)
Packit Service c82647
    fromcode_conv = (char *) alloca (fromcode_len);
Packit Service c82647
  else
Packit Service c82647
    {
Packit Service c82647
      fromcode_conv = (char *) malloc (fromcode_len);
Packit Service c82647
      if (fromcode_conv == NULL)
Packit Service c82647
	{
Packit Service c82647
	  if (! tocode_usealloca)
Packit Service c82647
	    free (tocode_conv);
Packit Service c82647
	  return (iconv_t) -1;
Packit Service c82647
	}
Packit Service c82647
    }
Packit Service c82647
  strip (fromcode_conv, fromcode);
Packit Service c82647
  fromcode = (fromcode_conv[2] == '\0' && fromcode[0] != '\0'
Packit Service c82647
	      ? upstr (fromcode_conv, fromcode) : fromcode_conv);
Packit 6c4009
Packit Service c82647
  __gconv_t cd;
Packit Service c82647
  int res = __gconv_open (tocode, fromcode, &cd, 0);
Packit 6c4009
Packit Service c82647
  if (! fromcode_usealloca)
Packit Service c82647
    free (fromcode_conv);
Packit Service c82647
  if (! tocode_usealloca)
Packit Service c82647
    free (tocode_conv);
Packit 6c4009
Packit 6c4009
  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
Packit 6c4009
    {
Packit 6c4009
      /* We must set the error number according to the specs.  */
Packit 6c4009
      if (res == __GCONV_NOCONV || res == __GCONV_NODB)
Packit 6c4009
	__set_errno (EINVAL);
Packit 6c4009
Packit 6c4009
      cd = (iconv_t) -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return (iconv_t) cd;
Packit 6c4009
}