libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

convert.c (2582B)


      1 /*
      2      This file is part of libextractor.
      3      Copyright (C) 2004 Vidyut Samanta and Christian Grothoff
      4 
      5      libextractor is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      libextractor is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libextractor; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #include "platform.h"
     22 #include "extractor.h"
     23 #include "convert.h"
     24 
     25 /**
     26  * Convert the len characters long character sequence
     27  * given in input that is in the given charset
     28  * to UTF-8.
     29  *
     30  * @param input string to convert
     31  * @param len number of bytes in input
     32  * @param charset input character set
     33  * @return the converted string (0-terminated), NULL on error
     34  * @return the converted string (0-terminated),
     35  *  if conversion fails, a copy of the original
     36  *  string is returned.
     37  */
     38 char *
     39 EXTRACTOR_common_convert_to_utf8 (const char *input,
     40                                   size_t len,
     41                                   const char *charset)
     42 {
     43 #if HAVE_ICONV
     44   size_t tmpSize;
     45   size_t finSize;
     46   size_t inSize;
     47   char *tmp;
     48   char *ret;
     49   char *itmp;
     50   const char *i;
     51   iconv_t cd;
     52 
     53   i = input;
     54   cd = iconv_open ("UTF-8", charset);
     55   if (cd == (iconv_t) -1)
     56     return strndup (i, len);
     57   if (len > 1024 * 1024)
     58   {
     59     iconv_close (cd);
     60     return NULL;   /* too big for meta data */
     61   }
     62   tmpSize = 3 * len + 4;
     63   tmp = malloc (tmpSize);
     64   if (NULL == tmp)
     65   {
     66     iconv_close (cd);
     67     return NULL;
     68   }
     69   itmp = tmp;
     70   finSize = tmpSize;
     71   inSize = len;
     72   if (iconv (cd,
     73              (char **) &input,
     74              &len,
     75              &itmp,
     76              &finSize) == ((size_t) -1))
     77   {
     78     iconv_close (cd);
     79     free (tmp);
     80     return strndup (i, inSize);
     81   }
     82   ret = malloc (tmpSize - finSize + 1);
     83   if (NULL == ret)
     84   {
     85     iconv_close (cd);
     86     free (tmp);
     87     return NULL;
     88   }
     89   memcpy (ret,
     90           tmp,
     91           tmpSize - finSize);
     92   ret[tmpSize - finSize] = '\0';
     93   free (tmp);
     94   iconv_close (cd);
     95   return ret;
     96 #else
     97   return strndup (input,
     98                   len);
     99 #endif
    100 }
    101 
    102 
    103 /* end of convert.c */