Back to index

glibc  2.9
iconv_open.c
Go to the documentation of this file.
00001 /* Get descriptor for character set conversion.
00002    Copyright (C) 1997,1998,1999,2000,2001,2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <alloca.h>
00022 #include <errno.h>
00023 #include <iconv.h>
00024 #include <stdbool.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 #include <gconv_int.h>
00029 #include "gconv_charset.h"
00030 
00031 
00032 iconv_t
00033 iconv_open (const char *tocode, const char *fromcode)
00034 {
00035   /* Normalize the name.  We remove all characters beside alpha-numeric,
00036      '_', '-', '/', '.', and ':'.  */
00037   size_t tocode_len = strlen (tocode) + 3;
00038   char *tocode_conv;
00039   bool tocode_usealloca = __libc_use_alloca (tocode_len);
00040   if (tocode_usealloca)
00041     tocode_conv = (char *) alloca (tocode_len);
00042   else
00043     {
00044       tocode_conv = (char *) malloc (tocode_len);
00045       if (tocode_conv == NULL)
00046        return (iconv_t) -1;
00047     }
00048   strip (tocode_conv, tocode);
00049   tocode = (tocode_conv[2] == '\0' && tocode[0] != '\0'
00050            ? upstr (tocode_conv, tocode) : tocode_conv);
00051 
00052   size_t fromcode_len = strlen (fromcode) + 3;
00053   char *fromcode_conv;
00054   bool fromcode_usealloca = __libc_use_alloca (fromcode_len);
00055   if (fromcode_usealloca)
00056     fromcode_conv = (char *) alloca (fromcode_len);
00057   else
00058     {
00059       fromcode_conv = (char *) malloc (fromcode_len);
00060       if (fromcode_conv == NULL)
00061        {
00062          if (! tocode_usealloca)
00063            free (tocode_conv);
00064          return (iconv_t) -1;
00065        }
00066     }
00067   strip (fromcode_conv, fromcode);
00068   fromcode = (fromcode_conv[2] == '\0' && fromcode[0] != '\0'
00069              ? upstr (fromcode_conv, fromcode) : fromcode_conv);
00070 
00071   __gconv_t cd;
00072   int res = __gconv_open (tocode, fromcode, &cd, 0);
00073 
00074   if (! fromcode_usealloca)
00075     free (fromcode_conv);
00076   if (! tocode_usealloca)
00077     free (tocode_conv);
00078 
00079   if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
00080     {
00081       /* We must set the error number according to the specs.  */
00082       if (res == __GCONV_NOCONV || res == __GCONV_NODB)
00083        __set_errno (EINVAL);
00084 
00085       cd = (iconv_t) -1;
00086     }
00087 
00088   return (iconv_t) cd;
00089 }