(root)/
man-db-2.12.0/
src/
convert.c
       1  /*
       2   * convert.c: simple encoding conversions
       3   *
       4   * Copyright (C) 2007-2022 Colin Watson.
       5   *
       6   * This file is part of man-db.
       7   *
       8   * man-db is free software; you can redistribute it and/or modify it
       9   * under the terms of the GNU General Public License as published by
      10   * the Free Software Foundation; either version 2 of the License, or
      11   * (at your option) any later version.
      12   *
      13   * man-db is distributed in the hope that it will be useful, but
      14   * WITHOUT ANY WARRANTY; without even the implied warranty of
      15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16   * GNU General Public License for more details.
      17   *
      18   * You should have received a copy of the GNU General Public License
      19   * along with man-db; if not, write to the Free Software Foundation,
      20   * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      21   */
      22  
      23  #ifdef HAVE_CONFIG_H
      24  #  include "config.h"
      25  #endif /* HAVE_CONFIG_H */
      26  
      27  #include <errno.h>
      28  #include <stdbool.h>
      29  #include <string.h>
      30  
      31  #ifdef HAVE_ICONV
      32  #  include <iconv.h>
      33  #endif /* HAVE_ICONV */
      34  
      35  #include "attribute.h"
      36  #include "xalloc.h"
      37  #include "xvasprintf.h"
      38  
      39  #include "manconfig.h"
      40  
      41  #include "cleanup.h"
      42  #include "encodings.h"
      43  
      44  #include "convert.h"
      45  
      46  #ifdef HAVE_ICONV
      47  static bool conv_to_locale_initialized = false;
      48  static iconv_t conv_to_locale = (iconv_t) -1;
      49  
      50  static void close_conv_to_locale (void *ignored MAYBE_UNUSED)
      51  {
      52  	iconv_close (conv_to_locale);
      53  }
      54  
      55  char * ATTRIBUTE_MALLOC convert_to_locale (char *string)
      56  {
      57  	if (!conv_to_locale_initialized) {
      58  		char *locale_charset = xasprintf
      59  			("%s//IGNORE", get_locale_charset ());
      60  		conv_to_locale = iconv_open (locale_charset, "UTF-8");
      61  		free (locale_charset);
      62  		if (conv_to_locale != (iconv_t) -1)
      63  			push_cleanup (close_conv_to_locale, NULL, 0);
      64  		conv_to_locale_initialized = true;
      65  	}
      66  
      67  	if (conv_to_locale != (iconv_t) -1) {
      68  		size_t string_conv_alloc = strlen (string) + 1;
      69  		char *string_conv = xmalloc (string_conv_alloc);
      70  		for (;;) {
      71  			char *inptr = string, *outptr = string_conv;
      72  			size_t inleft = strlen (string);
      73  			size_t outleft = string_conv_alloc - 1;
      74  			if (iconv (conv_to_locale,
      75  				   (ICONV_CONST char **) &inptr, &inleft,
      76  				   &outptr, &outleft) == (size_t) -1 &&
      77  			    errno == E2BIG) {
      78  				string_conv_alloc <<= 1;
      79  				string_conv = xrealloc (string_conv,
      80  							string_conv_alloc);
      81  			} else {
      82  				/* Either we succeeded, or we've done our
      83  				 * best; go ahead and print what we've got.
      84  				 */
      85  				string_conv[string_conv_alloc - 1 - outleft] =
      86  					'\0';
      87  				break;
      88  			}
      89  		}
      90  		return string_conv;
      91  	} else
      92  		return xstrdup (string);
      93  }
      94  #else /* !HAVE_ICONV */
      95  char * ATTRIBUTE_MALLOC convert_to_locale (char *string)
      96  {
      97  	return xstrdup (string);
      98  }
      99  #endif /* HAVE_ICONV */